//
//  ZYCModifyDutyRecordCell.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/23.
//

#import "ZYCModifyDutyRecordCell.h"
#import "ZYCDutyBulletionRecordDetailModel.h"
#import "ZYCAddPhotoPlaceHolderCell.h"
#import "ZYCPhotoCollectionCell.h"


#define maxContent 200
#define BTImageTotalNumber 9
#define ZYCContentPlaceholder @"请输入内容~"
#define ZYCContentPlaceholderColor [UIColor colorWithHexString:@"#BBBBBB" alpha:1.0]

@interface ZYCModifyDutyRecordCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UITextViewDelegate,ZYCPhotoCollectionCellDelegate>

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *line;
@property (nonatomic, strong) UITextView *contentTextView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@end
@implementation ZYCModifyDutyRecordCell

#pragma mark - setters
- (void)setModel:(ZYCBusRecordReportsModel *)model{
    _model = model;
    _titleLabel.text = model.quotaName;
    if (model.appContent.length == 0) {
        _contentTextView.text = ZYCContentPlaceholder;
        _contentTextView.textColor = ZYCContentPlaceholderColor;
    }else{
        _contentTextView.text = model.appContent;
        _contentTextView.textColor = HEX_RGB(@"#2B302D");
    }
    if ([model.complete isEqualToString:@"0"]) {
        [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
    }else{
        [_saveButton setTitle:@"保存成功" forState:UIControlStateNormal];
    }

    [self.collectionView reloadData];
}

#pragma mark - init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.saveButton];
        [self.contentView addSubview:self.line];
        [self.contentView addSubview:self.contentTextView];
        [self.contentView addSubview:self.collectionView];
        [self setupContaint];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}


#pragma mark - UICollectionViewDataSource && UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.model.busFileList.count == BTImageTotalNumber) {
        return self.model.busFileList.count;
    }else{
        return self.model.busFileList.count + 1;
    }

}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == self.model.busFileList.count) {
        ZYCAddPhotoPlaceHolderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZYCAddPhotoPlaceHolderCell class]) forIndexPath:indexPath];
        return cell;
    }else{
        ZYCPhotoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZYCPhotoCollectionCell class]) forIndexPath:indexPath];
        cell.delegate = self;
        NSArray *busFileList = self.model.busFileList;
        cell.model = busFileList[indexPath.item];
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == self.model.busFileList.count) {
        if ([self.delegate respondsToSelector:@selector(modifyDutyRecordCellClickAddPhoto:)]) {
            [self.delegate modifyDutyRecordCellClickAddPhoto:self.model];
        }
    }

}


#pragma mark - NSNotification method
- (void)textChanged:(NSNotification*)noti{


  //限制输入字数
    [self textViewMaxTextCount:self.contentTextView AndMaxCount:maxContent];
    
}

/// UITextView字数限制
/// @param textView textView
/// @param maxCount 限制字数
- (void)textViewMaxTextCount:(UITextView *)textView AndMaxCount:(int)maxCount{
    NSString *toBeString =textView.text;
    NSString *lang = [[UIApplication sharedApplication] textInputMode].primaryLanguage;
    if ([lang isEqualToString:@"zh-Hans"]) { // 中文输入
        UITextRange *selectedRange = [textView markedTextRange];
        // 获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        if (!position) {// 没有高亮选择的字，则对已输入的文字进行字数统计和限制
            if (toBeString.length > maxCount) {
                textView.text = [toBeString substringToIndex:maxCount];
                [MrjToast makeToast:@"字数不能超过200字" makeView:[UIApplication sharedApplication].keyWindow];
            }
        }else{//有高亮选择的字符串，则暂不对文字进行统计和限制
            
        }
    }else{//中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        if (toBeString.length > maxCount) {
            textView.text = [toBeString substringToIndex:maxCount];
            [MrjToast makeToast:@"字数不能超过200字" makeView:[UIApplication sharedApplication].keyWindow];
        }
    }
}

#pragma mark - UITextViewDelegate
-(void) textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length < 1) {
        textView.text = ZYCContentPlaceholder;
        textView.textColor = ZYCContentPlaceholderColor;
        self.model.appContent = @"";
    }else{
        self.model.appContent = textView.text;
    
    }
    if ([self.delegate respondsToSelector:@selector(modifyDutyRecordCellClickEditText:)]) {
        [self.delegate modifyDutyRecordCellClickEditText:self.model];
    }

}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:ZYCContentPlaceholder]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
}
#pragma mark - ZYCPhotoCollectionCellDelegate

- (void)photoCollectionCellDidClickDelete:(ZYCPhotoCollectionCell *)cell{

    [self.model.busFileList removeObject:cell.model];
    [self.collectionView reloadData];
    
    if ([self.delegate respondsToSelector:@selector(modifyDutyRecordCellClickDeletePhoto:)]) {
        [self.delegate modifyDutyRecordCellClickDeletePhoto:self.model];
    }
    
}

#pragma mark - action
- (void)clickSaveAction:(UIButton *)sender{
    [self.collectionView reloadData];
    if ([self.delegate respondsToSelector:@selector(modifyDutyRecordCellClickSave:)]) {
        [self.delegate modifyDutyRecordCellClickSave:self.model];
    }
}

#pragma mark - getters
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel LabelWithFont:kSystemBoldFont(17) titleColor:HEX_RGB(kTitleThemeColor) textAlignment:ZZTextAlignmentLeft];
    }
    return _titleLabel;
}

- (UIButton *)saveButton {
    if (!_saveButton) {
        _saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveButton setTitleColor:HEX_RGB(@"#2079D3") forState:UIControlStateNormal];
        _saveButton.titleLabel.font = kCustomFont(@"PingFangSC-Light", 17.0f);
        [_saveButton addTarget:self action:@selector(clickSaveAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}
- (UILabel *)line {
    if (!_line) {
        _line = [[UILabel alloc] init];
        _line.backgroundColor = HEX_RGB(@"#EAEAEA");
    }
    return _line;
}

- (UITextView *)contentTextView {
    if (!_contentTextView) {
        _contentTextView = [[UITextView alloc] init];
        _contentTextView.delegate = self;
        _contentTextView.font = kCustomFont(@"PingFangSC-Regular", 16.0f);
        _contentTextView.scrollEnabled = YES;
    }
    return _contentTextView;
}


- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[ZYCAddPhotoPlaceHolderCell class] forCellWithReuseIdentifier:NSStringFromClass([ZYCAddPhotoPlaceHolderCell class])];
        [_collectionView registerClass:[ZYCPhotoCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([ZYCPhotoCollectionCell class])];
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)layout{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.itemSize = CGSizeMake(98.0, 98.0f);
        _layout.minimumLineSpacing = 0;//行间距
        _layout.minimumInteritemSpacing = 0;//列间距
        _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return _layout;
}


#pragma mark - 约束
- (void)setupContaint{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.contentView).offset(15.0f);
    }];
    
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel);
        make.right.equalTo(self.contentView).offset(-15.0f);
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.top.equalTo(self.contentView).offset(47.0f);
        make.height.mas_equalTo(1.0f);
    }];
    
    [self.contentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line.mas_bottom).offset(25.0f);
        make.left.equalTo(self.contentView).offset(15.0f);
        make.right.equalTo(self.contentView).offset(-15.0f);
        make.height.mas_equalTo(100.0f);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentTextView.mas_bottom).offset(10.0f);
        make.left.equalTo(self.contentView).offset(15.0f);
        make.right.equalTo(self.contentView).offset(-15.0f);
        make.bottom.equalTo(self.contentView).offset(-15.0f);
        make.height.mas_equalTo(100.0f);
    }];
}
@end
